<template>

    <!-- 未曾登录钱包的界面 -->
    <div class="wallet-container" v-if="isLogin == false">

        <div class="wallet-header-menu" v-if="isShowMenu">
            <div class="wallet-header-menu-container">
               <div class="block_li">
                    <span class="arrow-down">{{ $setI18n('lang_selectLang','选择语言') }}</span>
                    <div class="picker-block">
                        <div class="li" @click="handleClickChangeLang(0)"><i></i>{{ $setI18n('lang_chinese','中文') }}</div>
                        <div class="li" @click="handleClickChangeLang(1)"><i></i>English</div>
                    </div>
               </div>
               <div class="block_li">
                    <span class="arrow-down">{{ $setI18n('lang_TestNet','测试网') }}</span>
               </div>
            </div>
            <div class="wallet-header-menu-mesk" @click="isShowMenu = false"></div>
        </div>

        <div class="wallet-header">

            <a href="/" class="logo">
                <img :src="getLogoImage" alt="" />
            </a>

            <div class="wallet-more-btn" @click="isShowMenu = true"></div>

            <div class="language-picker">
               <span class="arrow-down">{{ $setI18n('lang_selectLang','选择语言') }}</span>
               <div class="picker-block">
                   <div class="li" @click="handleClickChangeLang(0)">{{ $setI18n('lang_chinese','中文') }}</div>
                   <div class="li" @click="handleClickChangeLang(1)">English</div>
                </div>
            </div>

             <div class="language-picker language-picker-2">
                <span class="arrow-down">{{ $setI18n('lang_TestNet','测试网') }}</span>
            </div>

        </div>

        <div class="wallet-body1">
            <h1 class="welcome-title1">{{ $setI18n('lang_welcome','欢迎登陆Smartx钱包') }}</h1>
            <div class="wallet-funcs1">
                <div class="funcs-item1" @click="handleClickLoadWallet" >
                    <span class="funcs-title1" >{{ $setI18n('lang_comeinWallet','进入钱包') }}</span>
                    <p class="">{{ $setI18n('lang_comeinWallet_tips_1','如果已有钱包，请进入钱包') }}</p>
                 </div>
                 <div class="funcs-item2" @click="handleClickCreateWallet">
                    <span class="funcs-title2" >{{ $setI18n('lang_createWallet','创建新钱包') }}</span>
                    <p >{{ $setI18n('lang_comeinWallet_tips_2','如果没钱包，请创建新钱包') }}</p>
                 </div>
            </div>

        </div>
        <!-- 将导入私钥放在右下角，跟昨天的样式差不多。 -->
        <div>
            <!-- <div class="kongbai"></div> -->
            <input type="checkbox" @click="loginByKeys" v-model="checkVal" class="import"/>
            <div class="labelImport" >
                {{ $setI18n('lang_importWallet','导入私钥') }} <br />
            </div>
        </div>

        <div v-if="checkVal == true" :style="checkVal == true ? 'display:block':''">
            <textarea rows="4" cols="16"  class="importPrivateKeys" :placeholder="$setI18n('lang_importPrivateKeys','请输入私钥')"  v-model.trim="private_Key" > </textarea>
            <div class="loginByPrivateKey"  @click="LoginByPrivateKeys">{{ $setI18n('lang_importcheck','确认导入') }}</div>
        </div>




        <!-- <div class="wallet-body">
            <h1 class="welcome-title">{{ $setI18n('lang_welcome','欢迎登陆Smartx钱包') }}</h1>
            <div class="wallet-funcs">
                <div class="funcs-item  funcs-item-bg1" @click="handleClickLoadWallet">
                    <span class="funcs-title">{{ $setI18n('lang_comeinWallet','进入钱包') }}</span>
                    <p>{{ $setI18n('lang_comeinWallet_tips_1','如果已有钱包，请进入钱包') }}</p>
                </div>
                <div class="funcs-item  funcs-item-bg2" @click="handleClickCreateWallet">
                    <span class="funcs-title">{{ $setI18n('lang_createWallet','创建新钱包') }}</span>
                    <p>{{ $setI18n('lang_comeinWallet_tips_2','如果没钱包，请创建新钱包') }}</p>
                </div>
            </div>
        </div>

        <div >
            <div class="kongbai"></div>
            <input type="checkbox" @click="loginByKeys" v-model="checkVal" class="import"/>
            <div class="labelImport">
                Import Keys <br />
            </div>
        </div> -->

        <!-- 私钥登录界面 -->
          <!--<div v-if="checkVal == true" :style="checkVal == true ? 'display:block':''">
            <div class="labelPrivateKeys">privateKey:</div>
            <textarea rows="4" cols="16"  class="importPrivateKeys" :placeholder="'请输入私钥'"  v-model.trim="private_Key" > </textarea>
            <input type="button"  class="loginByPrivateKey" value="登录" @click="LoginByPrivateKeys">
        </div> -->






        <!-- 创建钱包输入密码界面 -->
        <div class="popUp-window"
             v-if="isCreateWallet == true && isLoadWallet == false"
             :style="isCreateWallet == true && isLoadWallet == false && isShowDownLoadJson == false ? 'display:block':''">

                <div class="window-wrapper pwd-container popUp-window-pwd">
                    <div class="window-content">
                        <p class="pwd-title">{{ $setI18n('lang_comeinWallet_password','密码') }}</p>
                        <div class="input-area" >
                            <input type="password"   :placeholder="$setI18n('lang_comeinWallet_password_placeholder','请输入密码')"     v-model.trim="pwd">
                            <div class="process-bar" :style="{'left': pwdStyle()}"></div>
                        </div>

                        <div class="pwd-level">{{ $setI18n('lang_comeinWallet_power','密码强度') }}：<span class="blue-font">{{ pwdDifficultyText }}</span></div>

                        <div class="input-area">
                            <input type="password" @keyup.enter="handleClickCreateWalletForWindow" :placeholder="$setI18n('lang_comeinWallet_password_placeholder_again','请再输入密码')" v-model.trim="pwd2">
                        </div>

                        <div class="common-btn enter-wallet" @click="handleClickCreateWalletForWindow">{{ $setI18n('lang_comeinWallet_text','创建钱包') }}</div>

                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
        </div>


        <!-- 登录钱包输入密码界面 -->
        <div class="popUp-window" v-if="isInputPwdWallet == true" :style="isInputPwdWallet == true ? 'display:block':''">

                <div class="window-wrapper pwd-container">
                    <div class="window-content">
                        <p class="pwd-title">{{ $setI18n('lang_comeinWallet_password','密码') }}</p>
                        <div class="input-area">
                            <input type="password" @keyup.enter="loadWalletFromFile" :placeholder="$setI18n('lang_comeinWallet_password_placeholder','请输入密码')"  v-model.trim="pwd3" />
                        </div>

                        <div class="common-btn enter-wallet" @click="loadWalletFromFile">{{ $setI18n('lang_comeinWallet','进入钱包') }}</div>

                    </div>
                </div>

                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
        </div>


        <!-- 上传keystore文件 -->
        <div class="popUp-window"  :style="isShowUpLoadWallet == true ? 'display:block !important':''">

                <div class="window-wrapper upload-keystore-file">
                    <div class="window-content">
                        <div class="circle-box">
                           <img :src="getBlackLogoPng" alt="" />
                        </div>
                        <p class="upload-tips">{{ $setI18n('lang_comeinWallet_pleaseUpload','请上传keystore文件') }}</p>
                        <div class="common-btn continue-btn">
                            <input type="file" class="inputFile"  @change="addFileJson" ref="inputFile" id="load"/>
                            {{ $setI18n('lang_comeinWallet_selectKs','选择keystore文件') }}
                        </div>
                        <div class="warning">{{ $setI18n('lang_comeinWallet_tips_2','如果没钱包，请创建新钱包') }}</div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
        </div>

        <!-- 成功创建钱包 -->
        <div class="popUp-window" v-if="isShowSuccessCreateWallet == true" :style="isShowSuccessCreateWallet == true ? 'display:block':''">
                <div class="window-wrapper success-deal create-wallet-success">
                    <div class="window-content">
                        <i class="success-icon"></i>
                        <p class="success-tips">{{ $setI18n('lang_comeinWallet_success_tip','成功创建钱包！') }}</p>
                        <div class="common-btn enter-wallet" @click="isShowSuccessCreateWallet = false">{{ $setI18n('lang_comeinWallet_ok','确定') }}</div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
        </div>

        <!-- 下载KeyStore文件 -->
        <div  class="popUp-window" v-if="isShowDownLoadJson == true" :style="isShowDownLoadJson == true ? 'display:block':''">
                <div  class="window-wrapper download-keystore-file">
                    <div class="window-content">
                        <div class="attetion-container">
                            <div class="attention-item">
                                <i class="no-lose-icon"></i>
                                <p class="title">{{ $setI18n('lang_comeinWallet_donot','不要丢失') }}</p>
                                <div class="description">
                                    {{ $setI18n('lang_comeinWallet_donot_tips','请注意，如果您丢失了文件，将不能恢复钱包') }}
                                </div>
                            </div>
                            <div class="attention-item">
                                <i class="no-share-icon"></i>
                                <p class="title">{{ $setI18n('lang_comeinWallet_donot_share','不要分享') }}</p>
                                <div class="description">
                                    {{ $setI18n('lang_comeinWallet_tips_3','如果你在欺诈网站使用文件，您的资金将会被偷走') }}
                                </div>
                            </div>
                            <div class="attention-item">
                                <i class="no-copy-icon"></i>
                                <p class="title">{{ $setI18n('lang_comeinWallet_backups','做好备份') }}</p>
                                <div class="description">
                                    {{ $setI18n('lang_comeinWallet_tips_4','确保文件安全，也许有一天它会价值千万美金') }}
                                </div>
                            </div>
                        </div>
                        <div class="common-btn download-btn" @click="createWalletFunc">{{ $setI18n('lang_comeinWallet_downloadKs','下载KeyStore文件') }}</div>
                        <div class="warning">{{ $setI18n('lang_comeinWallet_tips_5','如果已经拥有钱包，请进入钱包') }}</div>
                    </div>
                </div>

                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
            </div>
    </div>

   <!-- 已经登录钱包的界面 -->
   <div class="wallet-container" v-else>

        <div class="wallet-header-menu" v-if="isShowMenu2">
            <div class="wallet-header-menu-container">
               <div class="block_li">
                    <span class="arrow-down">{{ $setI18n('lang_selectLang','选择语言') }}</span>
                    <div class="picker-block">
                        <div class="li" @click="handleClickChangeLang(0)"><i></i>{{ $setI18n('lang_chinese','中文') }}</div>
                        <div class="li" @click="handleClickChangeLang(1)"><i></i>English</div>
                    </div>
               </div>
               <div class="block_li">
                    <span class="arrow-down">{{ $setI18n('lang_TestNet','测试网') }}</span>

               </div>
                <div class="block_li">
                      <span class="arrow-down" onclick="window.open('https://smartx.one')">{{ $setI18n('lang_website','官网') }}</span>
               </div>
               <div class="block_li">
                      <span class="arrow-down" onclick="window.open('https://miner.smartx.one')">{{ $setI18n('lang_water','水龙头') }}</span>
               </div>
            </div>
            <div class="wallet-header-menu-mesk" @click="isShowMenu2 = false"></div>
        </div>

        <div class="wallet-header white-header">

            <a href="/" class="logo"><img :src="getWhiteLogoPng" alt="" /></a>

            <div class="wallet-more-btn2" @click="isShowMenu2 = true"></div>
            <div class="wallet-user-btn" @click="isShowUserMenu = true"></div>

            <div class="language-picker">
                <span class="arrow-down">{{ $setI18n('lang_selectLang','选择语言') }}</span>
                <div class="picker-block">
                    <div class="li" @click="handleClickChangeLang(0)">{{ $setI18n('lang_chinese','中文') }}</div>
                    <div class="li" @click="handleClickChangeLang(1)">English</div>
                </div>
            </div>
             <div class="language-picker language-picker-2">
                <span class="arrow-down">{{ $setI18n('lang_TestNet','测试网') }}</span>

            </div>
            <div class="menu-picker-block">
                <div class="li" onclick="window.open('https://smartx.one')">{{ $setI18n('lang_website','官网') }}</div>
                <div class="li" onclick="window.open('https://miner.smartx.one')">{{ $setI18n('lang_water','水龙头') }}</div>
            </div>
        </div>
        <div class="wallet-body myWallet-container">

            <div class="myWallet-menu"  :style="isShowUserMenu ? 'display:block':''">
                <div class="user-infos">
                    <img :src="getUserDefaultPicPng" class="userPic" alt="" />
                    <p class="userName" :alt='address'>{{ address }}</p>
                </div>
                <ul class="menu-list">
                    <li @click="handleClickChangeMoneyList(false)" :class="isShowMoneyList == false ? 'active':''">{{ $setI18n('lang_myWallet','我的钱包') }}</li>
                    <li v-clipboard:copy="address"
                        v-clipboard:success="onCopySuccess"
                        v-clipboard:error="onCopyError">
                        {{ $setI18n('lang_copyUrl','复制地址') }}
                    </li>
                    <li @click="isShowQrcode = true">{{ $setI18n('lang_showUrl','显示地址') }}</li>
                    <li @click="isShowPrivateKeyWindow = true">{{ $setI18n('lang_getKey','获取私钥') }}</li>
                    <li @click="handleClickChangeMoneyList(true)" :class="isShowMoneyList == true ? 'active':''">{{ $setI18n('lang_transferRecord','最近转账记录') }}</li>
                    <li class="dot-icon" @click="handleLoginOut">{{ $setI18n('lang_closeWallet','关闭钱包') }}</li>
                </ul>

                <div class="menu-bottom">

                    <div class="li big" style="cursor: pointer;" onclick="window.open('https://smartx.one')">{{ $setI18n('lang_smartxWebsite','Smartx官网') }}</div>
                    <div class="li big">{{ $setI18n('lang_browser','区块链浏览器') }}</div>
                    <div class="li footer">Copyright 2019 <br/>Smartx All rights <br/>Reserved</div>

                </div>
            </div>
            <div class="myWallet-menu-mesk" :style="isShowUserMenu ? 'display:block':''" @click="isShowUserMenu=false"></div>
            <!-- 基本内容 -->

            <div class="myWallet-content">

              <div class="myWallet-topLine"></div>
              <div class="myWallet-content-scroll">

                <div class="myWallet_block" v-show="isShowMoneyList == false">
                    <h1 class="myWallet-title">{{ $setI18n('lang_myWallet','我的钱包') }}</h1>
                    <p class="myWallet-tip">{{ $setI18n('lang_comeinWallet_tips_6','欢迎来到Smartx世界') }}</p>

                    <div class="myWallet-box">
                        <div class="logo-name">
                            <img :src="getBlackLogoPng" alt="" />
                            <p>Smartx</p>
                        </div>
                        <div class="myWallet-money">
                            {{ $setI18n('lang_totalMoney','全部余额') }}<span class="amount" ref="amount">{{ amount }}</span>
                        </div>
                        <div class="common-btn send-btn" @click="handleClickSendMoney">{{ $setI18n('lang_send','发送') }}</div>
                        <div class="apply-test-coin">{{ $setI18n('lang_applyForTestCurrency','申请测试币') }}</div>
                    </div>

                    <div class="myWallet-box-bottom">
                        <div class="title">{{ $setI18n('lang_acknowledgementList','致谢名单') }}</div>
                        <div class="ballList">
                            <div class="li li_img_1"></div>
                            <div class="li li_img_2"></div>
                            <div class="li li_img_3"></div>
                            <div class="li li_img_4"></div>
                            <div class="li li_img_5"></div>
                        </div>
                        <div class="tips">{{ $setI18n('lang_comeinWallet_tips_7','感谢所有对 Smartx 区块链有过启发的产品和项目') }}</div>
                    </div>
                </div>

                <div class="myWallet_block" v-show="isShowMoneyList == true">

                    <div class="smart_container_rightBlock_title" style="text-align:left">
                        {{ $setI18n('lang_transferRecord','转账记录') }}
                        <span class="titleHash"></span>
                    </div>

                    <div class="smart_container_qkMessageBlock1">
                        <div class="smart_container_qkMessageBlock_left1">{{ $setI18n('lang_baseInfo','基础信息') }}</div>
                    </div>

                    <div class="smart_nav_list">
                        <div class="lis">
                            <div class="icon"></div>
                            <div class="rightText">
                                <span>{{ accountTransactionsCount }}</span>
                                <span>{{ $setI18n('lang_transactionNumber','交易笔数') }}</span>
                            </div>
                        </div>
                    </div>

                    <div class="smart_tableForAdress">
                        <div class="border">
                            <div class="border-scroll">
                                <table border="0" cellspacing="0" cellpadding="0">
                                    <thead>
                                        <tr>
                                            <th width='450' align='left' class="textIndent">{{ $setI18n('lang_hash','交易哈希') }}</th>
                                            <th align='left' >{{ $setI18n('lang_sender','发送方/接收方') }}</th>
                                            <th width='80' align="center">{{ $setI18n('lang_value','金额') }}</th>
                                            <th width='80' align="center">{{ $setI18n('lang_serviceCharge','手续费') }}</th>
                                            <th width='150' align="center">{{ $setI18n('lang_time','时间') }}</th>
                                            <th width='150' align="center">{{ }}</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr v-for="(_item, _index) in accountTransactionsList"
                                            :key="_index"
                                            >
                                            <td valign="middle">
                                                <div class="lis">
                                                    <div class="header"><img :src="getSmartxLogoPng" /></div>
                                                    <div class="content">
                                                        {{_item.hash}}
                                                    </div>
                                                </div>
                                            </td>
                                            <td valign="middle">
                                                <div class="labelLis">
                                                    <div class="label">
                                                        <span class="span1">from</span>
                                                        <span class="span2" :class="_item.addressIn==address ? 'blue':''">{{_item.addressIn}}</span>
                                                    </div>
                                                    <div class="label">
                                                        <span class="span1">to</span>
                                                        <span class="span2" :class="_item.addressOut==address ? 'blue':''">{{_item.addressOut}}</span>
                                                    </div>
                                                </div>
                                            </td>

                                            <td valign="middle">
                                                <strong>{{_item.amount}}</strong><br/>
                                                SAT
                                            </td>

                                            <td valign="middle">
                                                <strong>{{0}}</strong><br/>
                                                SAT
                                            </td>

                                            <td valign="middle">
                                                {{formatTime(_item.timestamp,'YYYY-MM-dd hh:mm:ss')}}
                                            </td>
                                            <td valign="middle">
                                                {{_item.temp==2?"转账失败":(_item.temp==0?"转帐中":"")}}
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div :class="(pageindex>0)? 'pageup' : 'pageup2' " @click="pageChange(1)"  :style="(pageindex>0)?'cursor:pointer':'cursor: not-allowed'" >
                        {{$setI18n('lang_br_prev','上一页')}}
                    </div>
                    <div :class="(accountTransactionsCount>((pageindex+1)*12))?'pagedown':'pagedown2'" @click="pageChange(0)" :style="(accountTransactionsCount>((pageindex+1)*12))?'cursor:pointer':'cursor: not-allowed'">
                        {{$setI18n('lang_br_next','下一页')}}
                    </div>

                </div>
                </div>
            </div>

            <!-- 私钥 -->
            <div class="popUp-window" v-if="isShowPrivateKeyWindow == true" :style="isShowPrivateKeyWindow == true ? 'display:block':''">
                <div class="window-wrapper secrete-key">
                    <div class="window-title">{{ $setI18n('lang_privateKey','私钥') }}</div>
                    <div class="window-content">
                        <div class="key">{{ privateKey }}</div>
                        <div class="key-tip">{{ $setI18n('lang_comeinWallet_tips_8','请不要截屏保存私钥') }}</div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
            </div>


            <!-- 申领测试币成功 -->
            <div class="popUp-window">
                <div class="window-wrapper apply-coin-success">
                    <div class="window-content">
                        <i class="success-icon"></i>
                        <div class="apply-success-tip">{{ $setI18n('lang_comeinWallet_tips_9','申领测试币成功！') }}</div>
                        <div class="apply-infos">
                            <p>{{ $setI18n('lang_comeinWallet_tips_10','请30s后查询账户余额变化。') }}SAT:</p>
                            <a href="javascript:;">FAFAAFAFNKASLFASNKNFAKSSNKNFAKS;LFJAS;FASF</a>
                        </div>
                        <div class="common-btn sure-btn">{{ $setI18n('lang_comeinWallet_ok','确定') }}</div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
            </div>


            <!-- 交易成功 -->
            <div class="popUp-window"   :style="isSendMoneySuccess == true ? 'display:block !important':''">
                <div class="window-wrapper success-deal" >
                    <div class="window-content">
                        <div class="success-icon"></div>
                        <div class="success-tips">{{ $setI18n('lang_comeinWallet_tips_11','交易已发送成功') }}</div>
                        <div class="common-btn sure-btn" @click="handleSuccessOk">{{ $setI18n('lang_comeinWallet_ok','确定') }}</div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
            </div>

            <!-- 发送交易 -->
            <div class="popUp-window" v-if="isShowSendMoney == true" :style="isShowSendMoney == true ? 'display:block':''">
                <div class="window-wrapper send-deal-container">
                    <div class="window-title">{{ $setI18n('lang_comeinWallet_tips_11s','交易已发送成功') }}</div>
                    <div class="window-content">
                        <div class="send-deal-title">
                            {{ $setI18n('lang_comeinWallet_from','从') }}<span class="gray-font"><strong>{{ from }}</strong></span>
                        </div>
                        <div class="avaliable-money">
                            <p><strong>{{ $setI18n('lang_comeinWallet_availableBalance','可用余额') }}&nbsp;&nbsp;{{ amount }}</strong></p>
                            <div class="money-input">
                                <input type="number" @keyup.enter="sendRawTransaction"  @keydown="handleInput2"  placeholder="0"   ref="allAmout"  v-model.trim="moneyNum" onfocus='if(this.value==0){this.value="";}; '  />
                                <span>
                                    SAT
                                </span>
                                <span class="transfer-all" @click="handleSetAllMoney">
                                    {{ $setI18n('lang_comeinWallet_transferAll','转出全部金额') }}
                                </span>
                            </div>
                        </div>
                        <div class="send-to-area">
                            <span>{{ $setI18n('lang_comeinWallet_to','发送至') }}</span>
                            <input type="text" @keyup.enter="sendRawTransaction" v-model.trim="to" />
                        </div>
                        <div class="gas-container">
                            <div class="gas-process-container">
                                <span>Gas</span>
                                <span class="gas-process">
                                    <i class="gas-process-bar"></i>
                                </span>
                            </div>
                            <div class="gas-input">
                                <input type="number" :placeholder="$setI18n('lang_comeinWallet_tips_17','请输入金额')"  value="0" ref="inputNumber" disabled/>
                                <span>Gas</span>
                            </div>
                        </div>
                        <div class="common-btn send-btn" @click="sendRawTransaction">{{ $setI18n('lang_send','发送') }}</div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
            </div>

            <!-- 二维码 -->
            <div class="popUp-window"  :style="isShowQrcode == true ? 'display:block !important':''">
                <div class="window-wrapper erweima-container">
                    <div class="window-content">
                        <div class="erweima-code">{{address}}</div>
                        <div class="erweima-pic" ref="code" id="code"></div>
                    </div>
                </div>
                <div class="popUp-window-mesk" @click="handleClickCloseWindow"></div>
            </div>

        </div>
    </div>


</template>

<script lang='ts'>
    declare let Wallet;
    declare let AES256;
    declare let Hashes;


import { Vue, Component, Prop, Watch } from "vue-property-decorator";

import { mapGetters, mapActions } from "vuex";

import { Buffer } from 'buffer';

import { Password } from '../../lib/password';

import { log, readInputFile } from '../../lib/utils';

import {Key} from 'smartxjs';

import { exec,exec2,geturl} from '../../lib/api';

import Utils from '@/utils/index';

import axios from "axios"

import {
    getKey,
    createNewWallet,
        WalletJson_V2_0_0,
        WalletState,
        validateWallet,
        GetWalletJson_V1_0_0,
    validatePassword,
    GlobalLoadedWallet,
} from "../../model/wallet";

/** 剪切板插件 */
import VueClipboard from 'vue-clipboard2';

import QRCode from "qrcodejs2";

/** 加载图片 */
import blackLogoPng      from '@/assets/images/public/logo.png';
import LogoPng           from '@/assets/images/public/black-logo.png';
import whiteLogoPng      from '@/assets/images/public/white-logo.png';
import userDefaultPicPng from '@/assets/images/public/user.svg';
import smartxLogoPng     from '@/assets/images/public/smartx-logo.png';
import erweimaPicPng     from '@/assets/images/public/erweima-pic.png';
import {Hash} from 'smartxjs';
import BigNumber from 'bignumber.js'

Vue.use(VueClipboard);

 interface TransferNonce{
    nonce: string;
    timestamp: string;
    random: string;
 }

@Component({
    methods:{
        ...mapActions([
            'getAccountTransactionsRequestAction',
            'getLatestBlockHeightRequestAction',
            'getAccountTransactionsCountAction'
        ])
    }
})
export default class WalletMain extends Vue {

   /** 图片 */
   private getBlackLogoPng:any      = blackLogoPng;
   private getLogoImage:any         = LogoPng;
   private getWhiteLogoPng:any      = whiteLogoPng;
   private getUserDefaultPicPng:any = userDefaultPicPng;
   private getSmartxLogoPng:any     = smartxLogoPng;
   private getErWeimaPicPng:any     = erweimaPicPng

   /** 判断是否登录 */
   private isLogin:Boolean  = false;

   /** 是否正在加载钱包; */
   private isLoadWallet:Boolean  = false;

   /** 是否正在创建钱包; */
   private isCreateWallet:Boolean = false;

   /** 是否显示download弹窗;  */
   private isShowDownLoadJson:Boolean = false;

   /** 是否显示创建钱包成功的弹窗; */
   private isShowSuccessCreateWallet:Boolean = false;

   /** 是否显示钱包上传弹窗 */
   private isShowUpLoadWallet:Boolean = false;

   /** 是否弹出输入密码加载钱包 */
   private isInputPwdWallet:Boolean = false;

   /** 是否显示发送交易窗口 */
   private isShowSendMoney:Boolean = false;

   /** 是否显示发送成功弹窗 */
   private isSendMoneySuccess:Boolean = false;

   /** 是否显示签名窗口 */
   private isShowPrivateKeyWindow:Boolean = false;

   /** 是否显示二维码 */
   private isShowQrcode:Boolean = false;

   /** 存储file对象 */
   private myFile:any     = null;

   /** 密码 */
   private pwd:string     = '';

   /** 确认密码 */
   private pwd2:string    = '';

   private checkVal:boolean =false;

   private private_Key:string = "";

   /** 密码 */
   private pwd3:string    = '';

   /**  */
   private privKey:string = '';

   /** pwd难度文字 */
   private pwdDifficultyText = this['$setI18n']('lang_common',"普通");

   private address:string = '';

   private from:string = '';

   private to:string   = '';

   private amount:BigNumber = new BigNumber(0);

   /** 转账金额 */
   private moneyNum:any  = 0;

   private key:any;

   private mySign:String  = '';
    //定时器1
   private clearInterval:any;
    //定时器2
   private clearInterval2:any;


   /** 私密key */
   private privateKey:string = '';
   /** 当前转账hash */
   private transferHashList:Array<any> = [];
   /** 当前转账链表 */
   private transferList:any=[];
   /** 当前转账msg */
   private transfermsg:Array<any> = [];
   /** 交易列表 */
   private accountTransactionsList:Array<any> = [];

   /** 交易笔数 */
   private accountTransactionsCount:Number = 0;

   /** 是否显示交易列表 */
   private isShowMoneyList:Boolean = false;

   /** 是否显示头部的导航 */
   private isShowMenu:Boolean = false;
   private isShowMenu2:Boolean = false;
   private isShowUserMenu:Boolean = false;
   private timestamp: any = 0;

   private nonce:number =0;
   private tranNum: any;
   private tranRes:Array<any>=[];
   private pageindex:number =0;
   /** 监听pwd变化 */
   @Watch('pwd',{ immediate: true, deep: true })
      onChangeForPwd(_new:string, _old:string){

        if(_new != undefined){

            /** 判断是否输入了中文，如果带有中文则删除 */
            if(/.*[\u4e00-\u9fa5]+.*$/g.test(_new)){
                 let reg = /[\u4e00-\u9fa5]/g;
                 this.pwd.replace(reg, "");
            }

            /** 去除前后空格 */
            this.pwd = this.pwd.replace(/(^\s*)|(\s*$)/g, "");
        }
   }

   /** 监听pwd变化 */
   @Watch('pwd2',{ immediate: true, deep: true })
      onChangeForPwd2(_new:string, _old:string){

        if(_new != undefined){

            /** 判断是否输入了中文，如果带有中文则删除 */
            if(/.*[\u4e00-\u9fa5]+.*$/g.test(_new)){
                 let reg = /[\u4e00-\u9fa5]/g;
                 this.pwd.replace(reg, "");
            }

            /** 去除前后空格 */
            this.pwd2 = this.pwd2.replace(/(^\s*)|(\s*$)/g, "");

        }
   }

   /** 监听pwd变化 */
   @Watch('pwd3',{ immediate: true, deep: true })
      onChangeForPwd3(_new:string, _old:string){

        if(_new != undefined){

            /** 判断是否输入了中文，如果带有中文则删除 */
            if(/.*[\u4e00-\u9fa5]+.*$/g.test(_new)){
                 let reg = /[\u4e00-\u9fa5]/g;
                 this.pwd3.replace(reg, "");
            }

            /** 去除前后空格 */
            this.pwd3 = this.pwd3.replace(/(^\s*)|(\s*$)/g, "");
        }
   }

   private  mounted():void {

       this.handleClickChangeLang(1);
   }


   private async handleClickChangeMoneyList(_isShowMoneyList:Boolean):Promise<void>{

         this.isShowMoneyList = _isShowMoneyList;

         if(document.body.clientWidth  <= 750 ){

             this.isShowUserMenu = false;
         }
         if(_isShowMoneyList==false){
         let _res: object = await exec('GET', `${Utils['judgingURLType']()}/?v1.0.0&cmd=balance&address=${this.address}`);

            if (_res != undefined) {
                this.amount =  new BigNumber(_res["available"]);
                // this.amount = parseInt(_res['available']);
                console.log(_res['available']);
                (this.$refs['amount'] as any).innerHTML = this.amount.toFormat();
            }
         }else{
            await this.getMoneyInfo(this.address);
            await this.checkNowTran();
         }
   }


   /** 切换语言 */
   private handleClickChangeLang(_type):void {

       if(_type == 0){

          this.$i18n['locale'] = 'zh-CN';

       }else if(_type == 1){

          this.$i18n['locale'] = 'en-US';

       }else if(_type == 2){

          this.$i18n['locale'] = 'en-ID';

       }else if(_type == 3){

          this.$i18n['locale'] = 'en-PH';

       }else if(_type == 4){

          this.$i18n['locale'] = 'ru-RU';
       }

       Utils['setSessionStorage']('lang', this.$i18n['locale']);
   }

   /** 转账成功 */
   private handleSuccessOk():void{

       (async()=>{

           /** 获取接口余额*/
           let _res:object = await  exec('GET',`${Utils['judgingURLType']()}?v1.0.0&cmd=balance&address=${ this.address }`);

           if(_res !=undefined){

              this.amount = new BigNumber(_res["available"]);

              (this.$refs['amount'] as any).innerHTML = this.amount;
           }
           await this.checkNowTran();

       });

      this.isSendMoneySuccess = false;

   }

   /** 销毁页面的时候，清除定时器 */
   private destroyed() {

       window.clearInterval(this.clearInterval);
       window.clearInterval(this.clearInterval2);
   };

   /** 设置全部金额 */
   private handleSetAllMoney():void{

        (this.$refs as any).allAmout.value = (this.amount>new BigNumber(0) ? this.amount : new BigNumber(0)).toFormat();

        this.moneyNum = (this.amount > new BigNumber(0)? this.amount : new BigNumber(0) );

   }

   /** 点击创建钱包按钮 */
   private handleClickCreateWallet():void{

            this.isCreateWallet = true;
            this.isLoadWallet   = false;
   }

   /** 点击进入钱包 */
   private handleClickLoadWallet():void{

            this.isCreateWallet = false;
            this.isLoadWallet   = true;

            this.isShowUpLoadWallet = true;
   }

   /** 点击弹窗创建钱包 */
   private handleClickCreateWalletForWindow():void{

            if(this.pwd != this.pwd2){

                alert(this['$setI18n']('lang_comeinWallet_tips_12',"两次输入的密码不一致，请重新输入!"));

                return;
            }
            else if (this.pwd=="") {
                //TODO: UI上提示用户密码不能为空
                alert("password is empty")
                return;
            }

            this.isShowDownLoadJson = true;
   }

   /** 关闭窗口(关闭窗口其实等于重置所有的状态) */
   private handleClickCloseWindow():void{

        /** 重置状态 */
        this.resetState();
   }

    /** 还原所有的状态 */
   private resetState():void{

        this.isCreateWallet     = false;
        this.isLoadWallet       = false;
        this.isShowDownLoadJson = false;
        this.isShowSuccessCreateWallet = false;
        this.isShowUpLoadWallet = false;
        this.isInputPwdWallet = false;
        this.isShowSendMoney = false;
        this.isSendMoneySuccess = false;
        this.isShowPrivateKeyWindow = false;
        this.isShowQrcode = false;

        this.pwd = '';
        this.pwd2= '';
        this.pwd3= '';

        this.moneyNum = 0;

        this.myFile = null;

        this.pwdDifficultyText  = this['$setI18n']('lang_common',"普通");

        // 重置input框
        var inputFile = this.$refs.inputFile;
        inputFile["type"] = 'text';
        inputFile["value"] = '';
        inputFile["type"] = 'file';
        inputFile["name"] = 'file';
   }


   /** 判断字符串中是否包含字母 */
   private hasLetter(str:String):Boolean {
    for (let i in str) {
        var asc = str.charCodeAt(parseInt(i));
        if ((asc >= 65 && asc <= 90 || asc >= 97 && asc <= 122)) {
            return true;
        }
    }
    return false;
   }

   /** 密码强度样式 */
   private pwdStyle():string{

       if(this.pwd.length < 3){

           this.pwdDifficultyText = this['$setI18n']('lang_common',"普通");
           return '0%';

       }else if(this.pwd.length > 3 && this.hasLetter(this.pwd) == false){

           this.pwdDifficultyText = this['$setI18n']('lang_secondary',"中等");
           return '33.3%';

       }else if(this.pwd.length >= 6 && this.hasLetter(this.pwd) == true){

           this.pwdDifficultyText = this['$setI18n']('lang_highStrength',"高强");'';
           return '66.6%';
       }

       return '0%';
   }


   /** 下载钱包 */
   private funDownload(content, filename):void {

            /** 创建隐藏的可下载链接 */
            console.log(content);
            let eleLink = document.createElement('a');

            eleLink.download = filename;

            eleLink.style.display = 'none';

            /** 字符内容转变成blob地址 */
            let blob = new Blob([content]);

            eleLink.href = URL.createObjectURL(blob);

            /** 触发点击 */
            document.body.appendChild(eleLink);

            eleLink.click();

            /** 然后移除 */
            document.body.removeChild(eleLink);
    };



    /** 加载json文件 */
    private addFileJson() {
           let _inputDOM = this.$refs.inputFile;
           /** 通过DOM取文件数据 */
           let _files = _inputDOM['files'];
           if (_files.length > 1) {
              alert(this['$setI18n']('lang_comeinWallet_tips_13','一次最多只能加载一个文件'));
              return;
           }
           /** 存储这个file对象; */
           this.myFile = _files[0];
           /** 重置状态 */
           //this.resetState();
           this.isShowUpLoadWallet = false;
           /** 输入密码弹窗; */
           this.isInputPwdWallet = true;
    }


    private inputFileById(id: string): Promise<string> {

            return readInputFile(document.getElementById(id) as HTMLInputElement);
    }


    private privateKeysOfTextarea(): string[] {

            if(this.privKey == null){

                console.log('private key input is null')
                return [];
            }

            return this.privKey == '' ? this.privKey.split(/\s/).map((e) => e.trim()).filter((e) => e) : []
    }

    // 创建钱包
    private createWalletFunc ():any {

        (async()=>{
                    var sat1= window.location.href.toString().split('=')[1];
                    console.log("create new wallet clicked");
                    console.log("password 1 " + this.pwd + " password 2 " + this.pwd2);

                    // 检查密码是否相等
                    const password = new Password(this.pwd);
                    const password2 = new Password(this.pwd2);

                    if (!password.equals(password2)) {
                        //TODO: UI上提示用户两次输入的密码不一致
                        alert("password do not match")
                        return null;
                    } else if (password.isEmpty()) {
                        //TODO: UI上提示用户密码不能为空
                        alert("password is empty")
                        return null;
                    } else {
                        //alert("password equals")
                    }


                    //let _res:object = await exec('GET',`${Utils['judgingURLType']()}/?v1.0.0&cmd=info`);
                    //let _network:string = _res['network'];

                    // 生成测试网的钱包
                    // TODO: createNewWallet的第二个参数，需要请求info接口获取
                    const wallet = createNewWallet(password);
                    console.log("create new wallet ");
                    console.log("version " + wallet.version);
                    console.log("address " + wallet.accounts[0].address);
                    var sat2=wallet.accounts[0].address;
                    var sign1 = sat1+sat2+"smartx2020";
                    var sign = new Hashes.SHA256().hex(sign1);
                     console.log("开始url");
                    var url = `${Utils['judgingURLType']()}/cmd100/reginvitaion/sat1=${sat1}&sat2=${sat2}&sign=${sign}`;

                    console.log(url);

                    let _res:object = await exec2('GET',url);

                    console.log(_res);

                    //赋值给全局的wallet
                    //TODO:这个全局对象需要有一种类似于cookie的机制，用户退出web钱包后，这个对象也需要被清理
                    const walletState = { wallet, password };
                    GlobalLoadedWallet.setWalletState(walletState);

                    //TODO: 跳转到下载链接页面walletDownload，让用户下载smartx-wallet.json

                    this.funDownload(JSON.stringify(wallet),'smartx-wallet.json');

                    /** 重置所有状态 */
                    this.resetState();

                    /** 弹出成功创建窗口 */
                    this.isShowSuccessCreateWallet = true;

                    return wallet;

         })();
    }

    /** 读取钱包 */
    private loadWalletFromFile() :any {

            console.log('load wallet clicked ');

            this.inputFileById('load').then((file)=>{

                        (async()=>{

                            // 校验钱包文件是否合法
                            console.log("get file json " + file);

                            try{
                                //let _res: object = await exec('GET', `${Utils['judgingURLType']()}/?v=1.0.0&cmd=info`);
                                //let _network: string = _res['network'];
                                var wallet: any = null;

                                const password = new Password(this.pwd3);

                                //TODO:testnet这里写死了，需要请求接口获取到main
                                var wallettemp = JSON.parse(file)
                                if (wallettemp != null && wallettemp.version < 101) {

                                    const { address, privKey, newWallet } = GetWalletJson_V1_0_0(file, password);
                                    wallet = newWallet;
                                }
                                else {
                                    wallet = validateWallet(file, "");
                                }

                                // 检查用户输入的密码是否合法
                                const walletState = validatePassword(wallet, password);

                                //TODO:这个全局对象需要有一种类似于cookie的机制，用户退出web钱包后，这个对象也需要被清理
                                GlobalLoadedWallet.setWalletState(walletState);

                                this.resetState();

                                //跳转到发送页面，测试一下读取到的钱包是否有效
                                //TODO:真正上线的时候，应该跳转到账户信息页面而不是发送页面
                                this.isLogin = true;

                                /** 初始化登录界面的数据 */
                                this.initLoginComplate();

                                return wallet;

                            }catch (e) {
                                console.log("e="+e);
                                if(e=="Error: Invalid password.")
                                {
                                    alert( this['$setI18n']('lang_errorPassword','密码错误！') );
                                }
                                else{
                                    alert( this['$setI18n']('lang_errorFile','导入非法文件！') );
                                    console.log(this['$setI18n']('lang_errorFile','导入非法文件！') );
                                    throw e;
                                }
                    }

                })();
            });
            return null;
    }


    /** 发送交易窗口 */
    private handleClickSendMoney():void{

         this.isShowSendMoney = true;
    }


    /** 退出当前登录状态 */
    private handleLoginOut():void{

         window.location.reload();
    }


    /** 初始化登录完成界面 */
    private initLoginComplate():void{

        (async()=>{

            //TODO: 请求服务器后端获取nonce和timestamp
            //let txNonce:TransferNonce = this.getTransferNonce();

            //获取到wallet里面的地址
            let walletState:WalletState = GlobalLoadedWallet.getWalletState();

            //如果没有加载钱包，重定向回去重新加载钱包;
            if(walletState == undefined){

                this.$router.push({path:'/brower/walletLogin'});

                return;
            }

            //TODO:这里暂时写死为第0个account,web钱包只支持一个account
            this.key = getKey(walletState,0);
            this.address = this.from = Wallet.ToAddress(this.key.publicKey);
            this.privateKey = Wallet.GetMnemonicWord(this.key);

            window.setTimeout(()=>{

                this.initQrcode();

            },1000);

            /** 获取接口余额*/
            this.nonce = await this.getTransferNonce(this.address);
            let _res: object = await exec('GET', `${Utils['judgingURLType']()}/?v1.0.0&cmd=balance&address=${this.address}`);

            if (_res != undefined) {
                this.amount = new BigNumber(_res['available']);
                //console.log(_res['available']);
                (this.$refs['amount'] as any).innerHTML = this.amount;
                /** 请求转账记录 */
                await this.getMoneyInfo(this.address);

                /** 循环30s读取钱包余额 */
                this.clearInterval = window.setInterval(async()=>{

                     let _res:object = await  exec('GET',
                                                `${Utils['judgingURLType']()}/?v1.0.0&cmd=balance&address=${ this.address }`);
                     if (_res !=undefined) {
                          this.amount = new BigNumber(_res['available']) > new BigNumber(0) ? new BigNumber(_res['available']) : new BigNumber(0) ;
                          (this.$refs['amount'] as any).innerHTML = this.amount.toFormat();
                          /** 请求转账记录 */
                          console.log(1);

                          await this.getMoneyInfo(this.address);
                     }
                     console.log(2);

                     await this.checkNowTran();

                },5000);
            }
        })();

    }


    /** 请求转账记录 */
    private async getMoneyInfo(_address):Promise<any>{

            /** 获取全网高度 */
            console.log("进入获取数据");
            console.log(this.pageindex);

            let _height = await this['getLatestBlockHeightRequestAction']();

            let _res =  await this['getAccountTransactionsRequestAction']({
                        address: _address,
                        height: _height,
                        index:this.pageindex
            });
            let res =  await this['getAccountTransactionsCountAction'](_address);
            console.log(this.transferHashList);

            for(let i=0;i<this.transferHashList.length;i++)
            {
                let res=await exec2('GET', `${Utils['judgingURLType']()}/?v1.0.0&cmd=block-by-tranhash&hash=${this.transferHashList[i][0].hash}`);
                this.tranRes.push(res);
            }
            console.log("tranRes=");

            console.log(this.tranRes);

            this.accountTransactionsCount = res.count;

            //console.log("_res.length="+_res.length);

            for( var i=0;i<_res.length;i++)
            {
                 var amount = new BigNumber((new BigNumber(_res[i].amount)).toFixed(4, BigNumber.ROUND_DOWN));
                 //console.log("amount="+amount);
                 _res[i].amount = amount;
            }

            this.accountTransactionsList   = _res;


            //console.log("time="+_res[0].timestamp);

            console.log(this.accountTransactionsList);


    }


    /** 生成二维码 */
    private initQrcode():Promise<any>{

      return new Promise((resolve, reject) => {

         let qrcode = new QRCode("code", {
          width: 200,
          height: 200,
          text: this.address,
          // 二维码地址
          colorDark: "#000",
          colorLight: "#fff"
        });

        resolve(qrcode);
      });
    }


    /** 请求服务器后端获取nonce和timestamp */
    private async getTransferNonce(address: string): Promise<number>{
        //console.log(`${Utils['judgingURLType']()}/?v1.0.0&cmd=getnonce&address=${address}`);
        const { success, message, nonce } = await exec2('GET', `${Utils['judgingURLType']()}/?v1.0.0&cmd=getnonce&address=${address}`);
        if (success)
            return nonce;
        return nonce;
    }


    /** 转换小数点四位 */
    private getFloat(x:any):any{

        return  x;

    }



    private handleInput2(e) {
        // 通过正则过滤小数点后八位
        if(e.code ==13)
        {
            this.sendRawTransaction();
            return;
        }
        e.target.value = (e.target.value.match(/^\d*(\.?\d{0,8})/g)[0]) || null


    }

    private async checkNowTran():Promise<any>
    {
        //temp转账结果 1成功 2失败 0正在转账
        //console.log("??????="+this.transferHashList.length);
        if(this.pageindex==0)
        {
            for( var i = 0; i < this.transferHashList.length; i++)
            {
                this.transferHashList[i][0].temp2+=1*5;  //计时器大于10分钟转账失败
                //console.log("temp2="+this.transferHashList[i][0].temp2)
                if(this.transferHashList[i][0].temp==1) //转账成功将之从列表删除
                {
                    //console.log("length="+this.transferHashList.length);
                    this.transferHashList.splice(i,1);
                    continue;
                }
                else if(this.transferHashList[i][0].temp2>600 &&this.transferHashList[i][0].temp!=2)//转账失败 显示转账失败
                {
                    this.transferHashList[i][0].temp=2;
                }
                if(this.transferHashList[i][0].temp!=2)//判断转账是否成功 成功就将标记显示成功
                {
                    const result = this.tranRes;

                    if(!(result[i]=="false"|| result[i]==false))
                    {
                        this.transferHashList[i][0].temp=1;
                        continue;
                    }
                }
                if(this.transferHashList[i][0].temp==0 && this.transferHashList[i][0].temp1==0){//未成功的，并且没有显示的添加到显示列表
                    for(var j=0;j<this.accountTransactionsList.length;j++)
                    {
                        // if(this.accountTransactionsList[j].hash==this.transferHashList[i][0].hash && this.accountTransactionsList[j].height!=null)
                        // {
                        //     this.transferHashList[i][0].temp=1;
                        // }
                        if(this.accountTransactionsList[j].hash==this.transferHashList[i][0].hash )
                        {
                            this.transferHashList[i][0].temp1=1;
                        }
                    }
                    if(this.transferHashList[i][0].temp!=1&&this.transferHashList[i][0].temp1==0)
                    this.accountTransactionsList.splice(0,0,this.transferHashList[i][0]);
                }
            }
        }
    }




    /** 发送交易 */
    private sendRawTransaction():void{

            //要求用户确认交易
            // const confirmed = window.confirm(
            //     `Are you sure you want to ${ this.from }\r\n ${ this.moneyNum }\r\n to \r\n ${ this.to } ?`,
            // );

            //打包交易
            //if (confirmed) {
                //console.log("aa");
            console.log(this.moneyNum);
            console.log("moneyNum");
            var amount = new BigNumber((new BigNumber(this.moneyNum)).toFixed(8, BigNumber.ROUND_DOWN)); // 截断小数点后8位，并且去掉多余的0
            console.log(amount);
            console.log("amount");
            if (amount.isLessThanOrEqualTo(0)) {
                alert('Please check your input amount');
                return ;
            }
            if (amount.isGreaterThan(this.amount)) {
                alert('Please check your input amount');
                return ;
            }
            if (amount.isNaN()) {
                alert('Please check your input amount');
                return ;
            }

            if(this.to == '' || !Wallet.CheckAddress(this.to)){
               alert(this['$setI18n']('lang_comeinWallet_tips_14','输入的地址不合法'));
               return;
            }

            if(this.to == this.address){
                alert(this['$setI18n']('lang_comeinWallet_tips_15','不能给自己转账'));
                return;
            }


            (async()=>{

                    this.nonce += 1;
                    console.log("nonce="+this.nonce);
                    var depend = "";
                    var remarks = "";
                    var addressCur = this.address;
                    var addressOut = this.to;
                    var type="transfer";
                    var data="";
                    var timestampNew = (new Date().getTime());
                    console.log(timestampNew+" .. "+this.timestamp);
                    if (timestampNew - 5 > this.timestamp) {
                        this.timestamp = timestampNew;
                    }
                    var hashdata = type + "#" + this.nonce + "#" + addressCur + "#" + addressOut + "#" + amount + "#" + data + "#" + depend + "#" + this.timestamp + "#" + remarks;
                    console.log("amount:"+amount);

                    var hash = new Hashes.SHA256().hex(hashdata);
                    var sign = Wallet.sign(hash, this.key);
                    var signHex = Wallet.Bytes2Hex(sign);

                    var transferdata = { type: type, hash: hash, nonce: this.nonce, addressIn: addressCur,addressOut: addressOut, amount: amount, data: data, depend: depend, timestamp: this.timestamp, sign: signHex, remarks: remarks }
                    var transfermsg = {};

                    transfermsg["hash"]=hash;
                    var temp=0;
                    transfermsg["temp"]=temp;
                    transfermsg["amount"]=amount;
                    transfermsg["addressIn"]=addressCur;
                    transfermsg["addressOut"]=addressOut;
                    transfermsg["timestamp"]=this.timestamp;
                    transfermsg["temp2"]=0;
                    transfermsg["temp1"]=0;
                    this.transferList.push(transfermsg);
                    this.transferHashList.push(this.transferList);
                    this.transferList=[];
                    console.log("transferHashList");
                    console.log(this.transferHashList)
                    //console.log(this.transferHashList[0].hash);

                    var _res = await geturl(`${Utils['judgingURLType']()}/?v1.0.0&cmd=Transfer`, transferdata);
                    //TODO:请求后端/transaction/raw接口
                    //console.log(_res);

                    if(_res !=undefined){

                        /** 关闭交易窗口 */
                        this.isShowSendMoney = false;

                        /** 显示交易成功窗口 */
                        this.isSendMoneySuccess = true;

                        /** 获取接口余额*/
                        let _res:object = await  exec('GET',`${Utils['judgingURLType']()}/?v1.0.0&cmd=balance&address=${ this.address }`);

                        if(_res !=undefined){

                             this.amount = new BigNumber(_res['available']) > new BigNumber(0) ? new BigNumber(_res['available']) : new BigNumber(0) ;

                            (this.$refs['amount'] as any).innerHTML = this.amount.toFormat() ;

                        }
                    }
            })();
            //}
    }



    /** 拷贝成功 */
    private onCopySuccess():void{
        alert(this['$setI18n']('lang_comeinWallet_tips_16','拷贝地址成功！'));
    }


    /** 拷贝失败 */
    private onCopyError():void{
        alert(this['$setI18n']('lang_comeinWallet_tips_17','拷贝地址失败！'));
    }

    /** 格式化时间 */
    private formatTime(_time,_format){

        //console.log("time"+_time);

        var d = new Date();
        // var offset = d.getTimezoneOffset() * 60000;
        _time = _time ;
        return  Utils['formatTime'](parseInt(_time),_format);
    }

    private loginByKeys(){
        if(!this.checkVal)
        {
            console.log("true");
        }
        else{
            console.log("false");
        }
    }
     /** 私钥随机数登录 */
    private LoginByPrivateKeys():any {

        // console.log(this.private_Key);
        if(this.private_Key.length==64 || this.private_Key.length==128 )
        {try{
                const password = new Password(this.private_Key);

                var keyPair = Wallet.ImportKeyPair(this.private_Key);
                var address = Wallet.ToAddress(keyPair.publicKey);

                const newWallet = new WalletJson_V2_0_0();
                newWallet.version = 101;
                newWallet.curIndex = 0;
                newWallet.accounts = [];

                var MnemonicWord = Wallet.GetMnemonicWord(keyPair);
                // console.log(password.value);
                // console.log(address);
                // console.log(MnemonicWord);
                const password2 = new Password("123");
                const encrypted = AES256.encrypt(MnemonicWord, password2.value);
                newWallet.accounts.push({ address, encrypted} );

                // console.log(JSON.stringify(newWallet));

                const walletState = validatePassword(newWallet, password2);
                GlobalLoadedWallet.setWalletState(walletState);
                this.resetState();
                //跳转到发送页面，测试一下读取到的钱包是否有效
                //TODO:真正上线的时候，应该跳转到账户信息页面而不是发送页面
                this.isLogin = true;

                /** 初始化登录界面的数据 */
                this.initLoginComplate();
            }
            catch(err)
            {
                alert(this['$setI18n']('lang_notice','请检查你的私钥！'));
            }
        }
        else{
            alert(this['$setI18n']('lang_notice','请检查你的私钥！'));
        }

    }

    /*上下翻页*/
    private async pageChange(index:number):Promise<any>
    {
        if(index==0)
        {
            if(this.accountTransactionsCount>(this.pageindex+1)*12)
            {
                this.pageindex+=1;
                await this.getMoneyInfo(this.address);
            }
        }
        else
        {
            if(this.pageindex>=1)
            {
                this.pageindex-=1;
                await this.getMoneyInfo(this.address);
            }
        }
    }


}

</script>

<style lang='scss'>
   @import 'wallet';
</style>
